﻿<%@ Page Language="C#" AutoEventWireup="true" EnableSessionState="True" %>

<%@ Register TagPrefix="Hi" Namespace="Hidistro.UI.SaleSystem.Tags" Assembly="Hidistro.UI.SaleSystem.Tags" %>
<%@ Register TagPrefix="Hi" Namespace="Hidistro.UI.Common.Controls" Assembly="Hidistro.UI.Common.Controls" %>
<%@ Register TagPrefix="wnxd" Namespace="wnxd.Web" Assembly="wnxd.Web" %>
<%@ Import Namespace="Hidistro.Entities.Sales" %>
<%@ Import Namespace="Hidistro.SaleSystem.Shopping" %>
<%@ Import Namespace="Hidistro.Entities.Orders" %>
<%@ Import Namespace="Hidistro.Messages" %>
<%@ Import Namespace="Hidistro.Membership.Context" %>
<%@ Import Namespace="Hidistro.Core" %>
<%@ Import Namespace="Hidistro.Membership.Core" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="Hidistro.Entities" %>
<%@ Import Namespace="wnxd.javascript" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Threading" %>
<html>
<head>
    <script type="text/javascript" src="http://res.juseshop.com/utility/jquery-1.6.4.min.js"></script>
    <script type="text/javascript" src="http://res.juseshop.com/utility/jquery.artDialog.js"></script>
    <link type="text/css" rel="stylesheet" href="http://res.juseshop.com/utility/skins/opera.css" />
</head>
<body>
    <wnxd:Web runat="server"></wnxd:Web>
</body>
</html>
<script runat="server">
    private void ErrorReturn(Web wnxd_web, string msg)
    {
        wnxd_web.AddScript("art.dialog({ title: \"提示\", content: \"" + msg + "\", cancel: true, fixed: true, close: function(){ location.href = \"" + this.Request.UrlReferrer.OriginalString + "#hot\" } });");
    }
    private bool IsMobilePhone(string input)
    {
        Regex regex = new Regex("^1[23456798][0-9]{9}$");
        return regex.IsMatch(input);
    }
    protected void Post()
    {
        Web wnxd_web = Web.FindFirst();
        int region;
        if (!int.TryParse(this.Request.Form["regionSelectorValue"], out region) || region == 0)
        {
            ErrorReturn(wnxd_web, "请选择收货地址");
            return;
        }
        string pattern = @"[\u4e00-\u9fa5a-zA-Z]+[\u4e00-\u9fa5_a-zA-Z0-9]*";
        Regex regex = new Regex(pattern);
        if (!(!string.IsNullOrEmpty(this.Request.Form["shr"]) && regex.IsMatch(this.Request.Form["shr"].Trim())))
        {
            ErrorReturn(wnxd_web, "请输入正确的收货人姓名");
            return;
        }
        if ((this.Request.Form["shr"].Length < 2) || (this.Request.Form["shr"].Length > 20))
        {
            ErrorReturn(wnxd_web, "收货人姓名长度应在2-20个字符之间");
            return;
        }
        if (string.IsNullOrEmpty(this.Request.Form["shdz"]))
        {
            ErrorReturn(wnxd_web, "请输入收货人详细地址");
            return;
        }
        if (this.Request.Form["shdz"].ToString().Length > 40)
        {
            ErrorReturn(wnxd_web, "输入地址字数过长");
            return;
        }

        if (!IsMobilePhone(this.Request.Form["sjhm"]))
        {
            ErrorReturn(wnxd_web, "请输入正确的手机号码");
            return;
        }
        string orderid = this.GenerateOrderId();
        int count = 1;
        if (!int.TryParse(this.Request.Form["count"], out count)) count = 1;
        IUser user = HiContext.Current.User;
        wnxd_OrderInfo OrderInfo = new wnxd_OrderInfo()
        {
            isSignBuy = true,
            Orderid = orderid,
            UserId = user.UserId,
            Username = user.Username,
            ShipTo = Globals.HtmlEncode(this.Request.Form["shr"]),
            Phone = this.Request.Form["sjhm"],
            Region = region,
            Address = this.Request.Form["shdz"],
            Remark = Globals.HtmlEncode(this.Request.Form["guest"]),
            ShippingModeId = 2,
            ShipToDate = "时间不限",
            PaymentTypeId = int.Parse(this.Request.Form["fkfs"]),
            shoppingCart = ShoppingCartProcessor.GetShoppingCart_danping(this.Request.Form["product"], count, true),
            //ShoppingCartProcessor.GetShoppingCart(this.Request.Form["product"], count, true),
            QQ = this.Request.Url.Host,
            SubmitDomain="http://"+this.Request.Url.Host,
        };
        HttpCookie cookie = HttpContext.Current.Request.Cookies["Source"];
        if (cookie != null) OrderInfo.Source = cookie.Value;
        if (this.Request.Form["fkfs"] == "2" || this.Request.Form["fkfs"] == "7")
        {
            switch (this.Request.Form["product"].ToString().Split('_')[1])
            {
                case "0":
                    OrderInfo.shoppingCart.ReducedPromotionAmount = 50 + 49;
                    break;
                case "3":
                    OrderInfo.shoppingCart.ReducedPromotionAmount = 200 + 199;
                    break;
                case "2":
                    OrderInfo.shoppingCart.ReducedPromotionAmount = 150 + 99;
                    break;
                case "1":
                    OrderInfo.shoppingCart.ReducedPromotionAmount = 100 + 69;
                    break;
            }
            OrderInfo.shoppingCart.IsReduced = true;
        }
        else
        {
            switch (this.Request.Form["product"].ToString().Split('_')[1])
            {
                case "0":
                    OrderInfo.shoppingCart.ReducedPromotionAmount = 49;
                    break;
                case "3":
                    OrderInfo.shoppingCart.ReducedPromotionAmount = 199;
                    break;
                case "2":
                    OrderInfo.shoppingCart.ReducedPromotionAmount = 99;
                    break;
                case "1":
                    OrderInfo.shoppingCart.ReducedPromotionAmount = 69;
                    break;
            }
            OrderInfo.shoppingCart.IsReduced = true;
        }
        string url = "http://order.x-juse.com/OrderProcess.aspx";
        new Thread(() =>
        {
            json json = new json(OrderInfo);
            WebRequest Request = WebRequest.Create(url);
            Request.Method = "POST";
            Request.ContentType = "application/x-www-form-urlencoded";
            byte[] data = Encoding.UTF8.GetBytes(json.ToString());
            Request.ContentLength = data.Length;
            using (Stream s = Request.GetRequestStream())
            {
                s.Write(data, 0, data.Length);
                s.Flush();
            }
            Request.BeginGetResponse(null, null);
            Request.Abort();
        }).Start();
        this.Response.Redirect("/Wait.aspx?orderid=" + orderid);
    }
    private string GenerateOrderId()
    {
        string str = string.Empty;
        Random random = new Random();
        for (int i = 0; i < 7; i++)
        {
            int num = random.Next();
            str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
        }
        int v = random.Next(0, 9);
        return (DateTime.Now.ToString("yyyyMMdd") + str + v);
    }
</script>
